/*
    Copyright 2010 Justin Lipton, Jonathan Rosenberg
    
    This file is part of MediaList.

    MediaList is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaList is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaList.  If not, see <http://www.gnu.org/licenses/>.
*/

var xmlhttp;

function Register(name,p1,p2,email, gender, month, day, year, location)
{
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  }
  
  var patt1=/[#\/?&\\<>,'"]/g;
  if(name.match(patt1))
  {
    document.getElementById("RegError").innerHTML="Name has characters not html friendly";
  }
  else if(p1.match(patt1))
  {
    document.getElementById("RegError").innerHTML="Password has characters not html friendly";
  }
  else if(p2.match(patt1))
  {
    document.getElementById("RegError").innerHTML="Passwords do not match";
  }
  else if(email.match(patt1))
  {
    document.getElementById("RegError").innerHTML="Email has characters not html friendly";
  }
  else if(location.match(patt1))
  {
    document.getElementById("RegError").innerHTML="Location has characters not html friendly";
  }
  else
  {
    var url="registerUser.php";
    url=url+"?name="+name;
    url=url+"&p1="+p1;
    url=url+"&p2="+p2;
    url=url+"&email="+email;
    url=url+"&gender="+gender;
    url=url+"&month="+month;
    url=url+"&day="+day;
    url=url+"&year="+year;
    url=url+"&location="+location;
    xmlhttp.onreadystatechange=regError;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

function regError()
{
  if (xmlhttp.readyState==4)
  {
    document.getElementById("RegError").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject)
  {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function submitenter(myfield,e,A,B,C,D,E,F,G,H,I)
{
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;

  if (keycode == 13)
  {
    Register(A,B,C,D,E,F,G,H,I);
    return false;
  }
  else
    return true;
}